package foxnarabbit;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.*;

public class Player extends JPanel implements ActionListener{

    public JLabel coords;
    private Timer timer;
    private ImageStore backdrop;
    private Playable playable;
    private NonPlayer npc1;
    
    public Player(){
        addKeyListener(new TAdapter());
        setFocusable(true);
        setBackground(Color.WHITE);
        setDoubleBuffered(true);
        coords = new JLabel("Hana Cute");
        add(coords, BorderLayout.AFTER_LAST_LINE);
        
        backdrop = new ImageStore("newfield.JPG");
        npc1 = new NonPlayer("deling.gif");
        playable = new Playable("angeling.gif");

        timer = new Timer(150, this);
        timer.start();
        }

   public void paint(Graphics g){
       super.paint(g);
       Graphics2D backg = (Graphics2D)g;
       Graphics2D play = (Graphics2D)g;
       Graphics2D nPlay = (Graphics2D)g;

       backg.drawImage(backdrop.getImage(), 0, 20, this);
       nPlay.drawImage(npc1.getImage(), npc1.getX(), npc1.getY(), this);
       play.drawImage(playable.getImage(), playable.getX(), playable.getY(), this);
       
       Toolkit.getDefaultToolkit().sync();
       g.dispose();

   }

   public void actionPerformed(ActionEvent e){
       playable.move();
       npc1.move();
       repaint();
       }

   
   private class TAdapter extends KeyAdapter{

       public void keyReleased(KeyEvent e){
           playable.keyReleased(e);
           //npc1.keyReleased(e);
           npc1.keyPressed(playable.getX(), playable.getY());
           PlayerLocation();
           CollisionDetector();
       }

       public void keyPressed(KeyEvent e){
           playable.keyPressed(e);
           //Thread.sleep(500);
           }
   }

   public void PlayerLocation(){
       coords.setText("Hana Cute says : x: " + playable.getX() + ", y: " + playable.getY());
       }
   public void CollisionDetector(){
       if(playable.getX() == npc1.getX() && playable.getY() == npc1.getY())
          coords.setText("Hana Cute says: Collision Detected");
   }
}